package com.ruoyi.crm.bid.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.bid.domain.CrmBidModelContent;
import com.ruoyi.crm.bid.mapper.CrmBidFileMapper;
import com.ruoyi.crm.bid.domain.CrmBidFile;
import com.ruoyi.crm.bid.service.ICrmBidFileService;

/**
 * 投标文件Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@Service
public class CrmBidFileServiceImpl implements ICrmBidFileService 
{
    @Autowired
    private CrmBidFileMapper crmBidFileMapper;

    /**
     * 查询投标文件
     * 
     * @param id 投标文件ID
     * @return 投标文件
     */
    @Override
    public CrmBidFile selectCrmBidFileById(Long id)
    {
        return crmBidFileMapper.selectCrmBidFileById(id);
    }

    /**
     * 查询投标文件列表
     * 
     * @param crmBidFile 投标文件
     * @return 投标文件
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "u")
    public List<CrmBidFile> selectCrmBidFileList(CrmBidFile crmBidFile)
    {
        return crmBidFileMapper.selectCrmBidFileList(crmBidFile);
    }

    /**
     * 新增投标文件
     * 
     * @param crmBidFile 投标文件
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCrmBidFile(CrmBidFile crmBidFile)
    {
    	crmBidFile.setCreateTime(DateUtils.getNowDate());
    	crmBidFile.setUpdateTime(DateUtils.getNowDate());
    	crmBidFile.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        int rows = crmBidFileMapper.insertCrmBidFile(crmBidFile);
        insertCrmBidModelContent(crmBidFile);
        return rows;
    }

    /**
     * 修改投标文件
     * 
     * @param crmBidFile 投标文件
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCrmBidFile(CrmBidFile crmBidFile)
    {
    	crmBidFile.setUpdateTime(DateUtils.getNowDate());
    	crmBidFile.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        crmBidFileMapper.deleteCrmBidModelContentByFileId(crmBidFile.getId());
        insertCrmBidModelContent(crmBidFile);
        return crmBidFileMapper.updateCrmBidFile(crmBidFile);
    }

    /**
     * 批量删除投标文件
     * 
     * @param ids 需要删除的投标文件ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCrmBidFileByIds(Long[] ids)
    {
        crmBidFileMapper.deleteCrmBidModelContentByFileIds(ids);
        return crmBidFileMapper.deleteCrmBidFileByIds(ids);
    }

    /**
     * 删除投标文件信息
     * 
     * @param id 投标文件ID
     * @return 结果
     */
    @Override
    public int deleteCrmBidFileById(Long id)
    {
        crmBidFileMapper.deleteCrmBidModelContentByFileId(id);
        return crmBidFileMapper.deleteCrmBidFileById(id);
    }

    /**
     * 新增模型字段内容信息
     * 
     * @param crmBidFile 投标文件对象
     */
    public void insertCrmBidModelContent(CrmBidFile crmBidFile)
    {
        List<CrmBidModelContent> crmBidModelContentList = crmBidFile.getCrmBidModelContentList();
        Long id = crmBidFile.getId();
        if (StringUtils.isNotNull(crmBidModelContentList))
        {
            List<CrmBidModelContent> list = new ArrayList<CrmBidModelContent>();
            for (CrmBidModelContent crmBidModelContent : crmBidModelContentList)
            {
                crmBidModelContent.setFileId(id);
                crmBidModelContent.setCreateTime(DateUtils.getNowDate());
                crmBidModelContent.setUpdateTime(DateUtils.getNowDate());
                crmBidModelContent.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
                crmBidModelContent.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
                list.add(crmBidModelContent);
            }
            if (list.size() > 0)
            {
                crmBidFileMapper.batchCrmBidModelContent(list);
            }
        }
    }
}
